package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjllmysybgRefVo;
import com.ruoyi.report_jl.domain.vo.ReportJlKfmdhslsyjlbBsInfoVo;
import com.ruoyi.report_jl.domain.vo.ReportJlKfsfsysxfjlbBsInfoVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlKflqhhlsybgBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlKflqhhlsybgBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlKflqhhlsybgBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlKflqhhlsybgBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlKflqhhlsybgBsInfoService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 矿粉试验检测报告Service业务层处理
 *
 * @author tll
 * @date 2024-06-28
 */
@RequiredArgsConstructor
@Service
public class ReportJlKflqhhlsybgBsInfoServiceImpl implements IReportJlKflqhhlsybgBsInfoService {

    private final ReportJlKflqhhlsybgBsInfoMapper baseMapper;

    private final TgsybgBaDetailMapper detailMapper;

    private static final Long[] keys = {64L, 65L, 66L, 67L, 68L, 69L, 70L};

    private final IBiQualificationService biQualificationService;

    /**
     * 查询矿粉试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportJlKflqhhlsybgBsInfoVo queryById(Long excelId) {
        // 主表
        ReportJlKflqhhlsybgBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId).orderByAsc(TgsybgBaDetail::getId));
        baDetailVos.forEach(v -> {
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        vo.setBaDetailVos(collect);
        List<ReportJlCjllmysybgRefVo> ssfs = new ArrayList<>(16);
        // 最大值 最小值
        String[] maxs = new String[]{"100", null, "100", "100", null, null, null, null, null};
        String[] mims = new String[]{"100", null, "90", "75", null, null, null, null, null};
        for (int i = 0; i < maxs.length; i++) {
            ReportJlCjllmysybgRefVo refVo = new ReportJlCjllmysybgRefVo();
            refVo.setMax(maxs[i]);
            refVo.setMin(mims[i]);
            ssfs.add(refVo);
        }
        // 水洗法-筛孔尺寸,通过百分率
        ReportJlKfsfsysxfjlbBsInfoVo sxVo = baseMapper.getDetailRelainfo(excelId);
        // 获取筛孔尺寸
        List<String> skcc = new ArrayList();
        String shaikccItem1 = sxVo != null ? sxVo.getShaikccItem1() : "";
        String shaikccItem2 = sxVo != null ? sxVo.getShaikccItem2() : "";
        String shaikccItem3 = sxVo != null ? sxVo.getShaikccItem3() : "";
        String shaikccItem4 = sxVo != null ? sxVo.getShaikccItem4() : "";

        skcc.add(shaikccItem1);
        skcc.add(shaikccItem2);
        skcc.add(shaikccItem3);
        skcc.add(shaikccItem4);

        // 获取通过百分率
        List<BigDecimal> bfl = new ArrayList<>();
        BigDecimal tonggbflPjzItem1 = sxVo != null ? sxVo.getTonggbflPjzItem1() : BigDecimal.ZERO;
        BigDecimal tonggbflPjzItem2 = sxVo != null ? sxVo.getTonggbflPjzItem2() : BigDecimal.ZERO;
        BigDecimal tonggbflPjzItem3 = sxVo != null ? sxVo.getTonggbflPjzItem3() : BigDecimal.ZERO;
        BigDecimal tonggbflPjzItem4 = sxVo != null ? sxVo.getTonggbflPjzItem4() : BigDecimal.ZERO;

        bfl.add(tonggbflPjzItem1);
        bfl.add(tonggbflPjzItem2);
        bfl.add(tonggbflPjzItem3);
        bfl.add(tonggbflPjzItem4);

        for (int i = 0; i < ssfs.size(); i++) {
            if (i < skcc.size()) {
                if (ObjectUtils.isNotEmpty(skcc.get(i))) {
                    ssfs.get(i).setSkcc(skcc.get(i));
                }
            }
            if (i < bfl.size()) {
                if (ObjectUtils.isNotEmpty(bfl.get(i))) {
                    ssfs.get(i).setScz(bfl.get(i));
                }
            }
        }
        vo.setSsfs(ssfs);
        // 加热安定性
        ReportJlKfmdhslsyjlbBsInfoVo mdRelainfo = baseMapper.getMdRelainfo(excelId);
        Map<String, Object> otObj = new HashMap<>();
        if (ObjectUtils.isNotEmpty(mdRelainfo) && ObjectUtils.isNotEmpty(mdRelainfo.getJiaradxsyysbhqkItem1())) {
            otObj.put("jiszb", "实测结论");
            otObj.put("jiancjg", mdRelainfo.getJiaradxsyysbhqkItem1());
            otObj.put("jiegpd", "符合");
        } else {
            otObj.put("jiszb", "-");
            otObj.put("jiancjg", "-");
            otObj.put("jiegpd", "-");
        }
        vo.setOtObj(otObj);
        return vo;
    }

    /**
     * 查询矿粉试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportJlKflqhhlsybgBsInfoVo> queryPageList(ReportJlKflqhhlsybgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlKflqhhlsybgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlKflqhhlsybgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询矿粉试验检测报告列表
     */
    @Override
    public List<ReportJlKflqhhlsybgBsInfoVo> queryList(ReportJlKflqhhlsybgBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlKflqhhlsybgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlKflqhhlsybgBsInfo> buildQueryWrapper(ReportJlKflqhhlsybgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlKflqhhlsybgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlKflqhhlsybgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlKflqhhlsybgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlKflqhhlsybgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlKflqhhlsybgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlKflqhhlsybgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlKflqhhlsybgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlKflqhhlsybgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlKflqhhlsybgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlKflqhhlsybgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlKflqhhlsybgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlKflqhhlsybgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlKflqhhlsybgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlKflqhhlsybgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlKflqhhlsybgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlKflqhhlsybgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlKflqhhlsybgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlKflqhhlsybgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlKflqhhlsybgBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlKflqhhlsybgBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlKflqhhlsybgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportJlKflqhhlsybgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportJlKflqhhlsybgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlKflqhhlsybgBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlKflqhhlsybgBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlKflqhhlsybgBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlKflqhhlsybgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportJlKflqhhlsybgBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(bo.getJincrq() != null, ReportJlKflqhhlsybgBsInfo::getJincrq, bo.getJincrq());
        lqw.eq(bo.getDaibsl() != null, ReportJlKflqhhlsybgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getChand()), ReportJlKflqhhlsybgBsInfo::getChand, bo.getChand());
        lqw.eq(bo.getRefExcelId() != null, ReportJlKflqhhlsybgBsInfo::getRefExcelId, bo.getRefExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlKflqhhlsybgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportJlKflqhhlsybgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlKflqhhlsybgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlKflqhhlsybgBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增矿粉试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportJlKflqhhlsybgBsInfoBo bo) {
        ReportJlKflqhhlsybgBsInfo add = BeanUtil.toBean(bo, ReportJlKflqhhlsybgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
//            ArrayList<TgsybgBaDetail> details = new ArrayList<>(15);
            for (Long key : keys) {
//                details.add(new TgsybgBaDetail(add.getExcelId(), key));
                detailMapper.insert(new TgsybgBaDetail(add.getExcelId(), key));
            }
//            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改矿粉试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportJlKflqhhlsybgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportJlKflqhhlsybgBsInfo update = BeanUtil.toBean(bo, ReportJlKflqhhlsybgBsInfo.class);
        validEntityBeforeSave(update);

        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportJlKflqhhlsybgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlKflqhhlsybgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除矿粉试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
